package com.cwf.common.handle;

import com.cwf.common.entity.http.R;
import com.cwf.common.enums.ResCodeEnum;
import com.cwf.common.exception.base.MyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@SuppressWarnings("all")
@RestControllerAdvice()
@Slf4j
public class ExceptionHandler {


    /**
     * @description: 单参数验证
     * @param e
     * @return: com.commons.utils.ResUtils
     * @author 18068
     * @date: 2022/9/3 10:51
     */
    @org.springframework.web.bind.annotation.ExceptionHandler({ConstraintViolationException.class})
    @ResponseBody
    public R exceptionHandler(ConstraintViolationException e) {
        List<Object> list = new ArrayList<>();
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();

        String msg = "";

        for (ConstraintViolation s:constraintViolations){

            String param = s.getPropertyPath().toString().split("\\.")[1];
            list.add(param+s.getMessage());

            msg += param+s.getMessage()+";";

        }

        return R.error(msg).writeCode(ResCodeEnum.PARAM_ERR.getCode());
    }

    /**
     * @description: 实体类验证
     * @param e
     * @return: com.commons.utils.ResUtils
     * @author 18068
     * @date: 2022/9/3 10:51
     */
    @org.springframework.web.bind.annotation.ExceptionHandler({MethodArgumentNotValidException.class})
    @ResponseBody
    public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        List result = new ArrayList();
        BindingResult bindingResult = e.getBindingResult();
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();

        String msg = "";

        for (FieldError fieldError:fieldErrors){
            result.add(fieldError.getField()+fieldError.getDefaultMessage());
            msg += fieldError.getField()+fieldError.getDefaultMessage()+";";
        }
        return R.error(msg).writeCode(ResCodeEnum.PARAM_ERR.getCode());
    }

    /**
     * @description: 异常捕获
     * @param e
     * @return: com.commons.utils.ResUtils
     * @author 18068
     * @date: 2022/9/3 11:11
     */
/*    @ExceptionHandler({Exception.class})
    @ResponseBody
    public ResUtils Exception(Exception e) {
        System.out.println(e);
        log.error(e.getMessage());
        return ResUtils.serviceError();
    }

    @ExceptionHandler({RuntimeException.class})
    @ResponseBody
    public ResUtils runException(RuntimeException e) {
        System.out.println(e);
        log.error(e.getMessage());
        return ResUtils.serviceError();
    }*/

    /**
     * @description: 自定义异常捕获
     * @param e
     * @return: com.commons.utils.ResUtils
     * @author 18068
     * @date: 2022/9/3 11:11
     */
    @org.springframework.web.bind.annotation.ExceptionHandler({MyException.class})
    @ResponseBody
    public R handleMethodArgumentNotValidException(MyException e) {
        System.out.println(e.getCode());
        return R.error().writeMsg(e.getMessage()).writeCode(e.getCode()).writeData(new ArrayList<>());
    }






}
